package com.carlinfo.shop.common.util;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池的工具类
 * @author wangshMac
 */
public class ExecutorServiceUtil
{
	/* 创建一个缓存的线程池 */
	public static ExecutorService executorService = Executors.newCachedThreadPool();
	
	/**
	 * 提交线程
	 * main方法不等待
	 */
	public static void submit(Callable callable)
	{
		executorService.submit(callable);
	}
	
	/**
	 * 提交线程
	 */
	public static void submit(List<Callable<Object>> callableList)
	{
		for (Iterator iterator = callableList.iterator(); iterator.hasNext();)
		{
			Callable<Object> callable = (Callable<Object>) iterator.next();
			executorService.submit(callable);
		}
	}
	
	/**
	 * 调用所有的线程
	 * main方法等待
	 */
	public static void invokeAll(List<Callable<Object>> callableList)
	{
		try
		{
			executorService.invokeAll(callableList);
		} catch (InterruptedException e)
		{
			ConstatFinalUtil.LOGGER.error("==线程invokeAll报错了==",e);
		}
	}
	
	/**
	 * 此方法永远不要调用
	 */
	public static void close()
	{
		executorService.shutdown();
	}
}
